package controller;

import domain.OrderResult;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.*;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.GridPane;
import service.FlowerStoreService;
import service.impl.FlowerStoreServiceImpl;
import utill.ParamUtil;
import utill.component.promptBox;
import utill.switchpages;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class StoreSelectOrderController {

    @FXML
    private TableView<OrderResult> TableViewShowOrder;

    @FXML
    private TextField GoutputIdText;

    @FXML
    private TableColumn<?, ?> viewflowertype;

    @FXML
    private Label GoutputLabelone;

    @FXML
    private TableColumn<?, ?> viewOrderDate;

    @FXML
    private TableColumn<?, ?> ViewStroneId;

    @FXML
    private Button btncommit;

    @FXML
    private TextField GoutputTotal;

    @FXML
    private Label GoutputLabelthree;

    @FXML
    private TextField Goutputpirce;

    @FXML
    private Label GoutputLabeltwo;

    @FXML
    private GridPane GoutputFactory;

    @FXML
    private Button btnClerar;

    @FXML
    private TableColumn<?, ?> viewFlowerPrice;

    @FXML
    private TableColumn<?, ?> viewOrderPirce;

    @FXML
    private Menu MenuSelectOrder;

    @FXML
    private AnchorPane ParentPane;

    @FXML
    private  MenuItem btnSelectOrder;

    @FXML
    private  TextField Goutputime;

    String LoginUrl="/controller/login.fxml";

    String StoreUrl="/controller/storeManage.fxml";

    FlowerStoreService flowerStoreService =new FlowerStoreServiceImpl();

    List<OrderResult> orderList=new ArrayList<>();
    @FXML
    void callback(ActionEvent event) throws Exception {
        switchpages switchpages =new switchpages();
        switchpages.switch_viewPane(this.ParentPane,this.StoreUrl);
    }

    @FXML
    void signOUt(ActionEvent event) throws Exception {
        ParamUtil.storeId.remove(Thread.currentThread().getName()+"storeId");
        switchpages switchpages =new switchpages();
        switchpages.switch_viewPane(this.ParentPane,this.LoginUrl);
    }

    @FXML
    void clear(ActionEvent event) {
        clearContext();
    }

    @FXML
    void commit(ActionEvent event) {
        if(orderList.size()==0){
            promptBox.tipsMessage("请查看订单后再尝试");
            return;
        }
       TableViewShowOrder.getItems().clear();
       Set<OrderResult> set= flowerStoreService.filterOrder(GoutputIdText.getText(),GoutputTotal.getText(),Goutputime.getText() ,orderList);


       bindTableView(TableViewShowOrder,ViewStroneId,viewflowertype,viewFlowerPrice,viewOrderPirce,viewOrderDate);

       ObservableList<OrderResult> data = FXCollections.observableArrayList();

       List<OrderResult> list =new ArrayList<>();
       for(OrderResult or : set){
            data.add(or);
       }
       TableViewShowOrder.setItems(data);

       promptBox.tipsMessage("查询完成");
       clearContext();
    }


    @FXML
    void SerlectOrder(ActionEvent event) {
       showData();
    }

    /**
     * 绑定tableview;
     * @param  tableView
     * @param  column1
     */
    public void bindTableView(TableView  tableView ,  TableColumn column1 ,
                              TableColumn column2 ,TableColumn column3,
                              TableColumn column4 ,TableColumn column5){
        column1.setCellValueFactory(new PropertyValueFactory<>("userName"));
        column2.setCellValueFactory(new PropertyValueFactory<>("flowerType"));
        column3.setCellValueFactory(new PropertyValueFactory<>("flowerPrice"));
        column4.setCellValueFactory(new PropertyValueFactory<>("flowertotal"));
        column5.setCellValueFactory(new PropertyValueFactory<>("flowerBuyTime"));
    }


    /**
     * 清理填框;
     */
    public  void clearContext(){
        GoutputIdText.setText("");
        GoutputTotal.setText("");
        Goutputime.setText("");
    }


    /**
     * 展示订单数据
     */
    public  void showData(){
        TableViewShowOrder.getItems().clear();
        orderList =flowerStoreService.orderList();
        bindTableView(TableViewShowOrder,ViewStroneId,viewflowertype,viewFlowerPrice,viewOrderPirce,viewOrderDate);
        ObservableList<OrderResult> data = FXCollections.observableArrayList();

        for(OrderResult o : orderList) {
            data.add(o);
        }
        TableViewShowOrder.setItems(data);
    }


    /**
     * 展示条件过滤后的数据;
     */
    public  void showData(List<OrderResult> list){
        TableViewShowOrder.getItems().clear();
        list =flowerStoreService.orderList();
        bindTableView(TableViewShowOrder,ViewStroneId,viewflowertype,viewFlowerPrice,viewOrderPirce,viewOrderDate);
        ObservableList<OrderResult> data = FXCollections.observableArrayList();

        for(OrderResult o : list) {
            data.add(o);
        }
        TableViewShowOrder.setItems(data);
    }

}
